package cn.chenjifeng.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;

import cn.chenjifeng.model.JsonObject;
import com.opensymphony.xwork2.ActionSupport;

import core.ImgUpload.ImgUpLoadAction;



public class ImgUpAction  extends ImgUpLoadAction
{
	/*
	 * 1.继承 ImgUpLoadAction
	 * 2.声明与jsp页面对应的文件变量
	 * 3.调用upload ，方法，传入存放图片的地址，以及其他3个参数
	 * */
	
	List<File> file;
	List<String> fileFileName;
	List<String> fileContextType;
	
	
	public String execute() throws IOException 
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		ArrayList<String> img_name = upload("/img",file,fileFileName,fileContextType);
		
		/*必须返回一个任意的json，这里返回的是文件的路径*/
		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		
		HashMap<String,Object> mp = new HashMap<String ,Object>();
		mp.put("file_id", img_name.get(0));
		
		PrintWriter pw = response.getWriter();
		pw.write(JSONArray.fromObject(new JsonObject(mp)).toString());
		return null;
	}
	
	
	
	

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContextType() {
		return fileContextType;
	}

	public void setFileContextType(List<String> fileContextType) {
		this.fileContextType = fileContextType;
	}
	
	
	
	
}
